import type { Router } from "express"
import type { Ioracle, IcallbackFn } from "../../types"

export default (router: Router, oracle: Ioracle, callbackFn: IcallbackFn) => {
  router.post("/getAssetList", (_req, res) => {
    let sql = " select dept_code,substr(dept_code, 1, 2) org_code "
    sql += " ,sum(nvl(decode(a.asset_type,'A',1,0),0)) A "
    sql += " ,sum(nvl(decode(a.asset_type,'Exist',1,0),0)) Exist "
    sql += " ,sum(nvl(decode(a.asset_type,'Demo',1,0),0)) Demo "
    sql += " ,sum(nvl( decode(a.asset_type,'Free',1,0),0)) Free "
    sql += " ,sum(nvl(decode(a.asset_type,'Offer',1,0),0)) Offer "
    sql += " ,sum(nvl(decode(a.asset_type,'Rent',1,0),0)) Rent "
    sql += " ,count(*) total "
    sql += " from xx_general_asset_equip_list  a "
    sql += " where 1 = 1 "
    sql += " group by dept_code "
    sql += " order by dept_code "
    callbackFn(res, oracle.connChaos, { sql })
  })

  router.post("/getAssetPlan", (req, res) => {
    const { checkMonth } = req.body

    let sql = " select * from xx_general_asset_check_header "
    //sql += " where check_month >= to_char(sysdate,'yyyymm') "
    sql += " where check_month >= '" + checkMonth + "' "
    sql += " order by check_month,dept_code "
    callbackFn(res, oracle.connChaos, { sql })
  })

  router.post("/removeAssetPlan", (req, res) => {
    const { checkMonth, deptCode } = req.body

    let sql = " delete from xx_general_asset_check_line "
    sql += " where header_id in ( "
    sql += " select header_id from xx_general_asset_check_header "
    sql += " where check_month = '" + checkMonth + "' "
    sql += " and dept_code = '" + deptCode + "' "
    sql += " ) "
    oracle
      .connChaos({ sql })
      .then(() => {
        sql = " delete from xx_general_asset_check_header "
        sql += " where check_month = '" + checkMonth + "' "
        sql += " and dept_code = '" + deptCode + "' "
        callbackFn(res, oracle.connChaos, { sql })
      })
      .catch((err) => {
        res.send({ code: 0, message: err })
      })
  })

  router.post("/getAssetDept", (_req, res) => {
    let sql = " select distinct dept_code "
    sql += " from xx_general_asset_equip_list "
    sql += " where dept_code not in ( "
    sql += " select dept_code "
    sql += " from xx_general_asset_check_header "
    sql += " where check_month >= to_char(sysdate,'yyyy') ||'01' "
    sql += " ) "
    sql += " order by dept_code "
    callbackFn(res, oracle.connChaos, { sql })
  })

  router.post("/createAssetPlan", (req, res) => {
    const { checkMonth, checkDept } = req.body
    let sql = ` begin xx_general_asset_pkg.xx_general_asset_create_p('${checkDept}','${checkMonth}');end;`
    callbackFn(res, oracle.connChaos, { sql })
  })

  router.post("/rewindAssetPlan", (req, res) => {
    const { checkMonth, checkDept } = req.body
    let sql = " declare "
    sql += " v_header varchar2(15); "
    sql += " begin "
    sql += " select header_id into v_header from xx_general_asset_check_header "
    sql += " where dept_code = '" + checkDept + "' "
    sql += " and check_month = '" + checkMonth + "'; "
    sql += " delete from xx_general_asset_check_flow "
    sql += " where header_id = v_header; "
    sql += " update xx_general_asset_check_header "
    sql += " set status = '初盤',current_seq ='' "
    sql += " ,flow_phase = '',updated_by = '' "
    sql += " ,updated_date = '',check_type = '' "
    sql += " where header_id = v_header; "
    sql += " end; "
    callbackFn(res, oracle.connChaos, { sql })
  })

  return router
}
